package com.ruoyi.work.service.impl;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.work.domain.WorkFill;
import com.ruoyi.work.domain.vo.MyFillRecord;
import com.ruoyi.work.domain.vo.MyProjectRecord;
import com.ruoyi.work.mapper.WorkRecordMapper;
import com.ruoyi.work.service.IWorkRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: MMDZ
 * @Desc:
 */
@Service
public class WorkRecordServiceImpl implements IWorkRecordService {

    @Autowired
    private WorkRecordMapper workRecordMapper;

    /**
     * 查询自己的填报记录（默认7天）
     *
     * @param record
     * @return
     */
    @Override
    public List<MyFillRecord> selectRecordList(MyFillRecord record) {
        Long userId = SecurityUtils.getUserId();
        String fillDate = null;
        if (StringUtils.isEmpty(record.getFillDate())) {
            fillDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);
            try {
                fillDate = new SimpleDateFormat(DateUtils.YYYY_MM_DD_HH_MM_SS).format(sdf.parse(record.getFillDate()));
            } catch (ParseException e) {
                e.printStackTrace();
                throw new ServiceException("日期转换异常，请联系管理员", HttpStatus.ERROR);
            }
        }
        return workRecordMapper.selectRecordList(userId, fillDate);
    }


    /**
     * 查询自己参加的项目
     *
     * @return
     */
    @Override
    public List<MyProjectRecord> selectMyProjectList(Long userId) {
        return workRecordMapper.selectMyProjectList(userId);
    }

    /**
     * 根据日期和个人用户查询填报记录
     *
     * @param record
     * @return
     */
    @Override
    public List<WorkFill> selectRecordByDataAndUserId(MyFillRecord record) {
        Long userId = SecurityUtils.getUserId();
        return workRecordMapper.selectRecordByDataAndUserId(userId, record.getFillDate());
    }
}
